package com.jn.erp.wechat.cp.handler;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.core.util.StrUtil;
import com.jn.erp.security.entity.SysUser;
//import com.jn.erp.web.api.sys.service.SysDictDataService;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.Gender;
import me.chanjar.weixin.cp.bean.WxCpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * <pre>
 *  企业微信员工配置
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/9/25 9:46
 */
@Component
public class CpUserHandler {

    private final WxCpService wxCpService;

    @Autowired
    public CpUserHandler(@Qualifier("wxCpUserService") WxCpService wxCpService){
        this.wxCpService = wxCpService;
    }

//    @Autowired
//    private SysDictDataService sysDictDataService;

    //新建用户
    public Boolean create(SysUser sysUser) {
        try {
            WxCpUser user = new WxCpUser();
//            String sexName = sysDictDataService.getDictDataNameByType("sys_user_sex", sysUser.getGender());
//            if (StrUtil.equals(sexName, "男")) {
//                user.setGender(Gender.MALE);
//            } else if (StrUtil.equals(sexName, "女")) {
//                user.setGender(Gender.FEMALE);
//            } else {
//                user.setGender(Gender.UNDEFINED);
//            }
            user.setUserId(sysUser.getName());
            user.setName(sysUser.getNickName());
            user.setDepartIds(new Long[]{sysUser.getOrgId()});
            user.setEmail(sysUser.getEmail());
            user.setMobile(sysUser.getMobile());
            wxCpService.getUserService().create(user);
            return true;
        } catch (WxErrorException e) {
            System.out.println(e.getMessage());
            return false;
        }
    }
    //删除用户
    public Boolean delete(SysUser sysUser) {
        try {
            wxCpService.getUserService().delete(sysUser.getName());
            return true;
        } catch (WxErrorException e) {
            System.out.println(e.getMessage());
            return false;
        }
    }


//    public Boolean getById(SysUser sysUser){
//        try {
//        WxCpUser user = wxCpService.getUserService().getById(sysUser.getId().toString());
//            System.out.println(user);
//        return true;
//        } catch (WxErrorException e) {
//            System.out.println(e.getMessage());
//            return false;
//        }
//    }


}
